import {click} from "@/api/game";

const game = {
    state: {
        clickResult: null,
        punishmentTtl: null,
        currentFreeCount: null,
        currentExtraCount: null
    },
    mutations: {
        SET_CLICK_RESULT: (state, clickResult) => {
            state.clickResult = clickResult
        },
        SET_PUNISHMENT_TTL: (state, ttl) => {
            state.punishmentTtl = ttl
        },
        SET_FREE_COUNT: (state, freeCount) => {
            state.currentFreeCount = freeCount
        },
        SET_EXTRA_COUNT: (state, extraCount) => {
            state.currentExtraCount = extraCount
        }
    },
    actions: {
        Click({commit}, username) {
            console.log(username)
            return new Promise((resolve, reject) =>{
                click(username).then(res => {
                    const { result, punishmentTtl } = res // 假设response.data是你的数据
                    const { freeCount, extraCount } = res.user
                    // 现在你可以使用result和punishmentTtl了
                    console.log('Result:', result)
                    console.log('Punishment TTL:', punishmentTtl)
                    if (result !== null && result !== undefined) {
                        commit('SET_CLICK_RESULT', result)
                    }
                    if (punishmentTtl !== null && punishmentTtl !== undefined) {
                        commit('SET_PUNISHMENT_TTL', punishmentTtl)
                    }
                    commit('SET_FREE_COUNT', freeCount)
                    commit('SET_EXTRA_COUNT', extraCount)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default game